package com.zcxy360.sandtable.mybatis;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.apache.ibatis.executor.result.ResultMapException;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

public class MapTypeHandler extends BaseTypeHandler<Map<String, Object>> {

	@Override
	public void setNonNullParameter(PreparedStatement ps, int i, Map<String, Object> parameter, JdbcType jdbcType)
			throws SQLException {
	}

	@Override
	public Map<String, Object> getNullableResult(ResultSet rs, String columnName) throws SQLException {
		Map<String, Object> map = new HashMap<>();
		map.put(columnName, rs.getObject(columnName));
		return map;
	}

	@Override
	public Map<String, Object> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
		return null;
	}

	@Override
	public Map<String, Object> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
		return null;
	}

	@Override
	public Map<String, Object> getResult(ResultSet rs, String columnName) throws SQLException {
		Map<String, Object> result;
		try {
			result = getNullableResult(rs, columnName);
		} catch (Exception e) {
			throw new ResultMapException(
					"Error attempting to get column '" + columnName + "' from result set.  Cause: " + e, e);
		}
		return result;
	}

}
